var app = getApp();
//var server = require('../../utils/server');
//
var url = "https://17fx.com/weapp/api/getshoplist.html";
//

var page_size = 10;
var Getglist = function (that,city_id,lbid) {
  wx.request({
    url: url,
    data: {
      openid: getApp().globalData.openid,
      city_id:city_id,
      lbid:lbid,
      page: that.data.page,
      page_size: page_size
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      console.log(res.data);
      if(res.data == null && that.data.page == 1){
        that.setData({
          listmsg:'抱歉暂时没有相关结果，换个筛选条件试试吧'
        })
      }else{
        that.setData({
          listmsg: null
        })
        var list = that.data.list;
        for (var i = 0; i < res.data.length; i++) {
          list.push(res.data[i]);
        }
        that.setData({
          list: list
        });

        that.setData({
          page: that.data.page + 1
        })
      }



    }

  })
}

var url2 = "https://17fx.com/weapp/api/getgoods_category.html";
//首页类别
var Getglist2 = function (that, city_id) {
  wx.request({
    url: url2,
    data: {
      openid: getApp().globalData.openid,
      city_id: city_id
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log(res.data)
      that.setData({
        lblist: res.data
      })
    }
  })
}


Page({
    data: {
        img_url: 'http://17fx.com/imgs/',
        filterdata: {},  //筛选条件数据
        showfilter: false, //是否显示下拉筛选
        showfilterindex: null, //显示哪个筛选类目
        cateindex: 0,  //一级分类索引
        cateid: null,  //一级分类id
        subcateindex: 0, //二级分类索引
        subcateid: null, //二级分类id
        areaindex: 0,  //一级城市索引
        areaid: null,  //一级城市id
        subareaindex: 0,  //二级城市索引
        subareaid: null, //二级城市id
        typeindex: 0,  //排序类型索引
        typeaid: null,  //排序类型id
        testdata: [], //服务集市列表
        scrolltop: null, //滚动位置
        lbid: 0 , 
        list:[],
        page:1,
        listmsg:null,
        lblist:[]
    },
    //上拉加载数据绑定
    onReachBottom: function (event) {
      console.log("上拉加载数据绑定")
      var that = this
      //调用getlist方法加载排行集合
      Getglist(that, getApp().globalData.city_id,that.data.lbid)
    },
    //下拉刷新
    onPullDownRefresh: function () {
      //调用getlist方法获取美女排行榜集合
      var that = this


    },
    onLoad: function (options) { //加载数据渲染页面
      console.log('页面传过来的参数' + options.id)

      var that = this
      that.setData({
        lbid:options.id
      })
      Getglist(that, getApp().globalData.city_id, that.data.lbid)
      Getglist2(that, getApp().globalData.city_id)
    },
    onScroll: function (e) {
      if (e.detail.scrollTop > 100 && !this.data.scrollDown) {
        this.setData({
          scrollDown: true
        });
      } else if (e.detail.scrollTop < 100 && this.data.scrollDown) {
        this.setData({
          scrollDown: false
        });
      }
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function (options) {
      
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    goToTop: function () { //回到顶部
        this.setData({
            scrolltop: 0
        })
    },


    toSearch: function () {
        wx.navigateTo({
            url: '../search/search',

        })
    },

    //商家类别
    toNearby: function (e) {
      var id = e.currentTarget.dataset.id;
      console.log('商家类别' + e.currentTarget.dataset.id);
      this.setData({
        list: [],
        page:  1,
        lbid: id
      });
      Getglist(this, getApp().globalData.city_id, id)
    },
});

